package client.heartbeat;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import client.TcpClient;

import util.ClientProtocol;

public class ClientHeartbeat {
	private TcpClient tc;
	private ScheduledExecutorService es = null;
	
	public ClientHeartbeat(TcpClient tc) {
		this.tc = tc;
	}
	
	public void start() {
		es = Executors.newSingleThreadScheduledExecutor();
		es.scheduleAtFixedRate(new Heartbeater(), ClientProtocol.HEARTGAP, ClientProtocol.HEARTGAP, TimeUnit.MILLISECONDS);
	}
	
	class Heartbeater implements Runnable {
		@Override
		public void run() {
			tc.sendMessage(ClientProtocol.HEARTBEAT);
		}
	}
	
}
